﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ActivityTracker.Library;
using Common.Library;


namespace ActivityTracker.Views.Activities
{
    public partial class AddPastHistoryFromPanorama : PhoneApplicationPage
    {
        #region Global

        Actvity objActivity = new Actvity();
        History objHistory = new History();
        string actId = "";
        string actName = "";
        List<ActivityProp> activityList;
        bool NewPageInstance = false;

        #endregion

        #region constructor

        public AddPastHistoryFromPanorama()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                activityList = objActivity.LoadActivities(5000);
                if (activityList.Count > 0)
                {
                    DataContext = activityList;
                    lpicActivities.SelectedIndex = App.lstPkrActivityid;
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In AddPastHistoryFromPanorama file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (lpicActivities.SelectedIndex != -1)
                {
                    actId = (lpicActivities.SelectedItem as ActivityProp).ActId;
                    actName = (lpicActivities.SelectedItem as ActivityProp).ActName;
                    if (actId != "" && actName != "")
                    {
                        DateTime startdate = Convert.ToDateTime(dpicDate.Value);
                        DateTime startTime = Convert.ToDateTime(dpicStartTime.Value);
                        DateTime EndTime = Convert.ToDateTime(dpicEndTime.Value);
                        objHistory.SaveActivity(actId, actName, startdate.ToShortDateString(), startTime.ToShortTimeString());
                        objHistory.SaveActivityHistory(SettingsHelper.getStringValue("hisId"), EndTime.ToShortTimeString(), Convert.ToInt32(tblkTotalDuration.Text));
                        objHistory.SaveTotalduration(actId, actName, Convert.ToInt32(tblkTotalDuration.Text), startdate.ToShortDateString());
                        NavigationService.Navigate(new Uri("/StartActivity.xaml", UriKind.Relative));
                    }
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarSave_Click Method In AddPastHistoryFromPanorama file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void dpicStartTime_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                TimeSpan st;
                st = Convert.ToDateTime(dpicEndTime.Value).Subtract(Convert.ToDateTime(dpicStartTime.Value));
                int TotalMinutes = 0;
                if (st.Hours > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Hours * 60);
                }
                if (st.Minutes > 0)
                {
                    TotalMinutes = TotalMinutes + st.Minutes;
                }
                if (st.Seconds > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Seconds / 60);
                }
                tblkTotalDuration.Text = TotalMinutes.ToString();
            }
            catch (Exception ex)
            {
                string mess = "Exception in dpicStartTime_ValueChanged Method In AddPastHistoryFromPanorama file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void dpicEndTime_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            try
            {
                TimeSpan st;
                st = Convert.ToDateTime(dpicEndTime.Value).Subtract(Convert.ToDateTime(dpicStartTime.Value));
                int TotalMinutes = 0;
                if (st.Hours > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Hours * 60);
                }
                if (st.Minutes > 0)
                {
                    TotalMinutes = TotalMinutes + st.Minutes;
                }
                if (st.Seconds > 0)
                {
                    TotalMinutes = TotalMinutes + (st.Seconds / 60) + 1;
                }
                tblkTotalDuration.Text = TotalMinutes.ToString();
            }
            catch (Exception ex)
            {
                string mess = "Exception in dpicEndTime_ValueChanged Method In AddPastHistoryFromPanorama file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lpicActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lpicActivities.SelectedIndex > 0)
            {
                NewPageInstance = true;
                if (NewPageInstance)
                    App.lstPkrActivityid = lpicActivities.SelectedIndex;
            }
        }

        private void AppBarHelp_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Help/Help.xaml?id=5&url=ovAaH4pOHzM", UriKind.Relative));
           
        }

        #endregion
    }
}